﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Collections.Specialized;

public partial class Request : System.Web.UI.Page
{
    SqlConnection conn = null;
    string idrequest = null;
    string iditem;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["Employee"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                conn = Database.GetConnection();
                idrequest = Request.QueryString["idrequest"].ToString();
                //if (!Page.IsPostBack)
                //{
                //    DateTime now = DateTime.Now;
                //    DisplayItems();
                //}
                //ListItemDetails();
                //GetListCategories();
                GetItem();
            } 
        }
        catch (Exception ex)
        {
            Response.Redirect("CreateRequest.aspx");
        }
    }   

    //public void GetListCategories()
    //{
    //    //SqlDataAdapter adapter = new SqlDataAdapter("select * from Categories", conn);
    //    //DataTable dt = new DataTable();
    //    //adapter.Fill(dt);
    //    //ddlCategories.DataSource = dt;
    //    //ddlCategories.DataTextField = "Cat_Name";
    //    //ddlItem.DataValueField = "Cat_ID";
    //    //ddlCategories.DataBind();
    //}
    //protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    //string idcatetogries = ddlCategories.SelectedValue.ToString();
    //    //if(conn.State == ConnectionState.Closed)
    //    //{
    //    //    conn.Open();
    //    //}
    //    //SqlCommand comm = new SqlCommand("select * from Item where Cat_ID = " + idcatetogries + "", conn);
    //    //SqlDataReader rd = comm.ExecuteReader();
    //    //ListItem list = null;
    //    //while (rd.Read())
    //    //{
    //    //    list = new ListItem(rd.GetString(3).ToString(), rd.GetInt32(0).ToString());
    //    //}
    //    //ddlItem.Items.Add(list);
    //    //SqlDataAdapter adapter = new SqlDataAdapter("select * from Item where Cat_ID = " + idcatetogries + "", conn);
    //    //DataTable dt = new DataTable();
    //    //adapter.Fill(dt);
    //    //ddlItem.DataSource = dt;
    //    //ddlItem.DataTextField = "I_Name";
    //    //ddlItem.DataValueField = "I_ID";
    //}
    protected void btnDone_Click(object sender, EventArgs e)
    {
        if(CheckItem() == true)
        {
            Response.Redirect("Homepage.aspx");
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string quantity = txbQuantity.Text;
        string iditem = ddlItem.SelectedValue.ToString();
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }
        SqlCommand comm = new SqlCommand("insert into ItemDetails values("+ddlItem.SelectedValue.ToString()+", "+idrequest+", "+txbQuantity.Text+")", conn);
        comm.ExecuteNonQuery();
        conn.Close();
        GetListItem();
    }
    public void GetListItem()
    {
        SqlDataAdapter adapter = new SqlDataAdapter("select ItemDetails.Detail_ID, Item.I_Name, ItemDetails.Quantity from  ItemDetails inner join Item on ItemDetails.I_ID = Item.I_ID where ItemDetails.R_ID = "+idrequest+"", conn);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        gvListItem.DataSource = dt;
        gvListItem.DataBind();
    }
    public void GetItem()
    {
        SqlDataAdapter adapter = new SqlDataAdapter("select * from Item", conn);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        if(!Page.IsPostBack)
        {
            ddlItem.DataSource = dt;
            ddlItem.DataValueField = "I_ID";
            ddlItem.DataTextField = "I_Name";
            ddlItem.DataBind();
        }
    }
    public bool CheckItem()
    {
        bool check = true;
        if (gvListItem.Rows.Count == 0)
        {
            lblCountItem.Text = "-no Item";
            check = false;
        }
        else 
        {
            check = true;
        }
        return check;
    }
    protected void gvListItem_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName=="Delete")
        {
            string iddetails = Convert.ToString(e.CommandArgument);
            if(conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            SqlCommand comm = new SqlCommand("delete from ItemDetails where Detail_ID = "+iddetails+"",conn);
            comm.ExecuteNonQuery();
            conn.Close();
        }
    }
}